﻿using SharpConfig;

namespace SyncFileCommon.BLL
{
    public class ConfigBLL
    {
        /// <summary>
        /// 服务端配置
        /// </summary>
        public static ConfigModel ServiceConfig;

        /// <summary>
        /// 客户端配置
        /// </summary>
        public static ClientConfigModel ClientConfig;

        /// <summary>
        /// 日志数据库信息
        /// </summary>
        public static DBConfigModel LogDBConfig;

        public static string ConfigFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + System.Diagnostics.Process.GetCurrentProcess().ProcessName + @"Config.ini";

        /// <summary>
        /// 加载配置文件信息
        /// </summary>
        /// <param name="configPath"></param>
        /// <returns></returns>
        public static bool LoadConfig(string configPath = "")
        {
            if (!string.IsNullOrWhiteSpace(configPath))
            {
                ConfigFilePath = configPath;
            }

            string _configValue = string.Empty;

            if (File.Exists(ConfigFilePath))
            {
                _configValue = File.ReadAllText(ConfigFilePath);
            }

            if (string.IsNullOrWhiteSpace(_configValue))
            {
                ServiceConfig = new ConfigModel();
                ClientConfig = new ClientConfigModel();
                LogDBConfig = new DBConfigModel();
                SaveConfig();
                return false;
            }
            else
            {
                Configuration _cfg = Configuration.LoadFromString(_configValue);
                ServiceConfig = _cfg["Service"].ToObject<ConfigModel>();
                ClientConfig = _cfg["Client"].ToObject<ClientConfigModel>();
                LogDBConfig = _cfg["LogDB"].ToObject<DBConfigModel>();

                _cfg = new Configuration();
                _cfg.Add(Section.FromObject("Service", ServiceConfig), "服务端配置");
                _cfg.Add(Section.FromObject("Client", ClientConfig), "客户端配置");
                _cfg.Add(Section.FromObject("LogDB", LogDBConfig), "日志数据库配置");

                if (_configValue != _cfg.StringRepresentation)
                {
                    SaveConfig();
                }
                return true;
            }

        }

        /// <summary>
        /// 保存配置文件信息
        /// </summary>
        public static void SaveConfig()
        {
            if (string.IsNullOrWhiteSpace(ConfigFilePath))
            {
                return;
            }

            if (File.Exists(ConfigFilePath))
            {
                File.Delete(ConfigFilePath);
            }

            if (ServiceConfig == null)
            {
                ServiceConfig = new ConfigModel();
            }

            if (ClientConfig == null)
            {
                ClientConfig = new ClientConfigModel();
            }

            if (LogDBConfig == null)
            {
                LogDBConfig = new DBConfigModel();
                LogDBConfig.DBPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) + @"\log";
            }

            Configuration _cfg = new Configuration();
            _cfg.Add(Section.FromObject("Service", ServiceConfig), "服务端配置");
            _cfg.Add(Section.FromObject("Client", ClientConfig), "客户端配置");
            _cfg.Add(Section.FromObject("LogDB", LogDBConfig), "日志数据库配置");
            _cfg.SaveToFile(ConfigFilePath);
        }


    }

    /// <summary>
    /// 服务端配置
    /// </summary>
    public class ConfigModel
    {

        /// <summary>
        /// 服务监听端口号
        /// </summary>
        [SharpConfig.Comment("服务监听端口号")]
        public int ServicePort { get; set; } = 59866;

        /// <summary>
        ///  Windows服务名称
        /// </summary>
        [SharpConfig.Comment("Windows服务名称")]
        public string WindowsServiceName { get; set; } = "JL_SyncFileService";

        /// <summary>
        ///  Windows服务名称
        /// </summary>
        [SharpConfig.Comment("Windows服务说明")]
        public string WindowsServiceDescription { get; set; } = "文件同步服务端";

        /// <summary>
        ///  服务器硬盘编号
        /// </summary>
        [SharpConfig.Comment("服务器硬盘编号")]
        public string DiskNumber { get; set; } = "";
 
    }


    /// <summary>
    /// 客户端配置
    /// </summary>
    public class ClientConfigModel
    {
        /// <summary>
        /// 删除文件备份文件夹路径
        /// </summary>
        [SharpConfig.Comment("删除文件备份文件夹路径")]
        public string DeleteFileDir { get; set; } = "";

        /// <summary>
        /// 删除文件备份多少天后自动清除
        /// </summary>
        [SharpConfig.Comment("删除文件备份多少天后自动清除")]
        public int DeleteFileSaveDay { get; set; } = 7;

        /// <summary>
        /// 同步计划字符串
        /// </summary>
        [SharpConfig.Comment("同步计划列表")]
        public string SyncFilePlanList { get; set; } = "";
    }


    /// <summary>
    /// 请求日志数据库信息配置
    /// </summary>
    public class DBConfigModel
    {
        /// <summary>
        /// 是否记录客户端访问日志
        /// </summary>
        [SharpConfig.Comment("是否记录客户端访问日志")]
        public bool IsRequestLog { get; set; } = true;

        /// <summary>
        /// 是否记录请求结果内容
        /// </summary>
        [SharpConfig.Comment("是否记录请求结果内容")]
        public bool IsResponseLog { get; set; } = true;

        /// <summary>
        /// 记录请求结果内容最大长度
        /// </summary>
        [SharpConfig.Comment("记录请求结果内容最大长度")]
        public int MaxResponseLog { get; set; } = 500;

        /// <summary>
        /// 数据库类型 SQLite，SQLServer，MySQL
        /// </summary>
        [SharpConfig.Comment("数据库类型 SQLite，SQLServer，MySQL")]
        public string DBType { get; set; } = "SQLite";

        /// <summary>
        /// SQLite数据库存放文件夹
        /// </summary>
        [SharpConfig.Comment("SQLite数据库存放文件夹")]
        public string DBPath { get; set; } = string.Empty;

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        [SharpConfig.Comment("数据库连接字符串")]
        public string ConnectionString { get; set; } = string.Empty;
    }

}
